package ltd.bugs.cute.urm.helper;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.util.WebUtils;

import ltd.bugs.cute.urm.jwt.PermJwtParams;
import ltd.bugs.cute.urm.manager.UserManager;
import ltd.bugs.cute.urm.model.User;

@Component
public class PermHelper {

  @Autowired
  private UserManager userManager;

  /**
   * 从http request的token中解析出对应的User
   * 
   * @param request
   * @return
   */
  public User getUserFromRequest(HttpServletRequest request) {
    Cookie cookie = WebUtils.getCookie(request, PermJwtParams.COOKIE_NAME);
    if (cookie == null) {
      return null;
    }

    return userManager.authentication(cookie.getValue());
  }
}
